<template>
    <div class="power-cost-list">

        <div class="page-header">
            <!-- <router-link class="back-link" to="/main/renshi/renshizhidu/dianfeihs/dianfeihs">
                <i class="el-icon-arrow-left"></i>
                返回
            </router-link> -->
            <el-button type="primary" style="margin-right: 20px;"
                @click="$router.push('/main/renshi/renshizhidu/dianfeihs/dianfeihs')" icon="el-icon-back">返回</el-button>

            <h2>发电费用核算表列表</h2>
        </div>

        <div class="search-container">
            <input v-model="searchForm.loadName" placeholder="请输入负荷名称搜索" />
            <button @click="handleSearch">搜索</button>
            <button @click="handleAdd">新增</button>
            <!-- <button @click="goBack">返回</button> -->
        </div>

        <div class="search-container">
            <table border="1" style="width: 100%; text-align: center;">
                <thead>
                    <tr>
                        <th><input type="checkbox" v-model="isAllSelected" @change="toggleSelectAll"></th>
                        <th>序号</th>
                        <th>负荷</th>
                        <th>每小时费用</th>
                        <th>每度电费用</th>
                        <th>每月发电度数</th>
                        <th>每月发电费用</th>
                        <th>备注</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item, index) in tableData" :key="item.id">
                        <td><input type="checkbox" v-model="selectedIds" :value="item.id"></td>
                        <td>{{ (current - 1) * size + index + 1 }}</td>
                        <td>{{ item.loadName }}</td>
                        <td>{{ item.hourlyCost }}</td>
                        <td>{{ item.perKwhCost }}</td>
                        <td>{{ item.monthlyPowerGeneration }}</td>
                        <td>{{ item.monthlyPowerCost }}</td>
                        <td>{{ item.remarks }}</td>
                        <td>
                            <button @click="handleView(item)">查看</button>
                            <button @click="handleEdit(item)">编辑</button>
                            <button @click="handleDelete(item.id)">删除</button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

        <div class="pagination">
            <button :disabled="current === 1" @click="handlePageChange(current - 1)">上一页</button>
            <span>第 {{ current }} 页 / 共 {{ total }} 条</span>
            <button :disabled="current * size >= total" @click="handlePageChange(current + 1)">下一页</button>
        </div>
    </div>
</template>

<script>
import { getPowerCostPage, deletePowerCost, batchDeletePowerCost } from '@/api/powerCost'

export default {
    data() {
        return {
            searchForm: {
                loadName: ''
            },
            tableData: [],
            current: 1,
            size: 10,
            total: 0,
            selectedIds: [],
            isAllSelected: false
        }
    },
    created() {
        this.fetchData()
    },
    watch: {
        selectedIds(newValue) {
            this.isAllSelected = newValue.length === this.tableData.length
        }
    },
    methods: {
        async fetchData() {
            try {
                const result = await getPowerCostPage(this.current, this.size, this.searchForm.loadName)
                this.tableData = result.data.records
                this.total = result.data.total
            } catch (error) {
                console.error('获取数据失败', error)
                this.$message.error('获取数据失败')
            }
        },
        handleSearch() {
            this.current = 1
            this.fetchData()
        },
        goBack() {
            this.$router.push('/main/renshi/renshizhidu/dianfeihs/dianfeihs')
        },
        handleAdd() {
            this.$router.push('/main/renshi/renshizhidu/dianfeihs/dianfeihs')
        },
        handleView(row) {
            this.$router.push({
                path: '/main/renshi/renshizhidu/dianfeihs/dianfeihs',
                query: { id: row.id, type: 'view' }
            })
        },
        handleEdit(row) {
            this.$router.push({
                path: '/main/renshi/renshizhidu/dianfeihs/dianfeihs',
                query: { id: row.id, type: 'edit' }
            })
        },
        async handleDelete(id) {
            try {
                await this.$confirm('确认删除该记录吗？')
                const result = await deletePowerCost(id)
                if (result) {
                    this.$message.success('删除成功')
                    this.fetchData()
                }
            } catch (error) {
                if (error !== 'cancel') {
                    console.error('删除失败', error)
                    this.$message.error('删除失败')
                }
            }
        },
        async handlePageChange(page) {
            this.current = page
            await this.fetchData()
        },
        toggleSelectAll() {
            if (this.isAllSelected) {
                this.selectedIds = this.tableData.map(item => item.id)
            } else {
                this.selectedIds = []
            }
        }
    }
}
</script>

<style scoped>
/* 容器样式 */
.power-cost-list {
    padding: 20px;
    /* background-color: #f5f7fa; */
    border-radius: 8px;
    /* box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); */
    min-height: calc(100vh - 40px);
    width: 1093px;
    margin: 0 auto;
}

/* 页面头部样式 */
.page-header {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    background-color: #fff;
    padding: 0px 24px;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

/* 返回链接样式 */
.back-link {
    display: flex;
    align-items: center;
    text-decoration: none;
    color: #409EFF;
    font-size: 16px;
    margin-right: 20px;
    transition: all 0.3s;
}

.back-link i {
    margin-right: 5px;
}

.back-link:hover {
    opacity: 0.8;
}

/* 标题样式 */
.page-header h2 {
    margin: 0;
    font-size: 22px;
    font-weight: 500;
    color: #303133;
}


.search-container {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
    background-color: #fff;
    padding: 20px 10px;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.search-container input {
    margin-right: 10px;
    padding: 5px;
    border: 1px solid #ccc;
}

.search-container button {
    margin-right: 10px;
}

table {
    border-collapse: collapse;
    margin-bottom: 20px;
}

th,
td {
    padding: 8px;
    border: 1px solid #ddd;
}

.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    background-color: #fff;
    padding: 20px 10px;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

button {
    padding: 8px 15px;
    cursor: pointer;
    background-color: #409eff;
    color: white;
    border: none;
    border-radius: 4px;
    transition: background-color 0.3s;
    font-family: '宋体';
    font-size: 1em;
    margin-left: 10px;
}

button:hover {
    background-color: #66b1ff;
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.6;
}
</style>